//
//  UITextFieldEditViewController.swift
//  FutureBull
//
//  Created by IronMan on 2021/3/17.
//  Copyright © 2021 wuyanwei. All rights reserved.
//
// MARK:- UITextField 编辑的类
import UIKit

enum FBFieldEditType {
    /// 昵称
    case userNickName
}

/// 编辑后的闭包
typealias EditTextClosure = (String) -> Void

class UITextFieldEditViewController: UIViewController {
    
    /// 编辑后的闭包
    var editTextClosure: EditTextClosure?
    /// 编辑的类型
    private var editType: FBFieldEditType?
    /// 头部标题
    private var titleName: String = ""
    /// 默认内容
    private var content: String?
    /// 占位符
    private var placeholder: String = ""
    /// 输入的最多字符
    private var maxCharacters: Int = 0
    /// 内容的输入规则
    private var regex: String?
    
    lazy var textFiled: UITextField = {
        let filed = UITextField()
        filed.placeholder = placeholder
        filed.font = UIFont.systemFont(ofSize: 16)
        filed.textAlignment = .left
        filed.clearButtonMode = .whileEditing
        filed.textColor = .c444444
        filed.delegate = self
        filed.addTarget(self, action: #selector(textChange), for: .allEditingEvents)
        if let weakContent = content {
            filed.text = weakContent
        }
        return filed
    }()
    
    /// 完成的颜色
    lazy var doneButtonItem: UIBarButtonItem = {
        let buttonItem = UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(doneClick))
        buttonItem.tintColor = .blue
        buttonItem.isEnabled = false
        return buttonItem
    }()
    
    init(editType: FBFieldEditType, titleName: String, placeholder: String, maxCharacters: Int, content: String?, regex: String? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.editType = editType
        self.titleName = titleName
        self.placeholder = placeholder
        self.maxCharacters = maxCharacters
        self.regex = regex
        self.content = content
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navUI()
        initUI()
        commonUI()
        updateTheme()
    }
    
    /// 导航栏的设置
    private func navUI() {

        self.view.backgroundColor = UIColor.white
        
        let navTitleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
        navTitleLabel.font = UIFont.systemFont(ofSize: 19)
        navTitleLabel.textAlignment = .center
        navTitleLabel.textColor = UIColor.c444444
        navTitleLabel.text = titleName
        self.navigationItem.titleView = navTitleLabel
        
        self.navigationItem.rightBarButtonItem = doneButtonItem
    }
    
    /// 创建控件
    private func initUI() {
        let backView = UIView(frame: CGRect(x: 0, y: kNavFrameH + 10, width: kScreenW, height: 55))
        backView.backgroundColor = UIColor.white
        self.view.addSubview(backView)
        backView.addSubview(textFiled)
        
        textFiled.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(22)
            make.centerY.equalToSuperview()
        }
        
        textFiled.becomeFirstResponder()
    }
    
    /// 添加控件和设置约束
    private func commonUI() {
        
    }
    
    /// 更新控件的颜色，字体，背景色等等
    private func updateTheme() {
        
    }
    
    @objc func doneClick() {
        guard let text = textFiled.text, text.count > 0 else {
            // "请输入内容".fb_toast()
            return
        }
        // 昵称
        if editType == .userNickName {
            updateUserNickName()
            return
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        textFiled.resignFirstResponder()
    }
}

// MARK:- 完成接口的调用
extension UITextFieldEditViewController {
    // MARK: 更新用户的昵称
    func updateUserNickName() {
        
    }
}

extension UITextFieldEditViewController: UITextFieldDelegate {
    
    @objc func textChange(textField: UITextField) {
   
        guard let text = textField.text else {
            return
        }
        // 没有修改内容颜色不可修改
        doneButtonItem.tintColor = text == content ? UIColor.gray : UIColor.blue
        doneButtonItem.isEnabled = text == content ? false : true
    }
    
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        return true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return textFiled.jk.inputRestrictions(shouldChangeTextIn: range, replacementText: string, maxCharacters: maxCharacters, regex: regex)
    }
    
    // 获得焦点
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true
    }
    
    // 失去焦点
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        return true
    }
}
